package db

import (
	"fmt"
	"hr/api/dto"
)

type PageModel struct {
	Total    int64       `json:"total"`
	Result   []map[string]interface{} `json:"result"`
	PageNo   int         `json:"pageNo"`
	PageSize int         `json:"pageSize"`
}

func GetApprovalRecordPaging(pagination dto.Pagination, id, no, types string) (PageModel, error) {
	result := PageModel{
		Total:    0,
		Result:   make([]map[string]interface{}, 0),
		PageNo:   pagination.PageNo,
		PageSize: pagination.PageSize,
	}
	offset := (pagination.PageNo - 1) * pagination.PageSize
	if types == "1" {
		sql := makeSql("approval_formal", id, offset,pagination.PageSize )
		if err := GetInstance().Raw(sql[0]).Scan(&result.Result).Error; err != nil {
			return result, err
		}
		if err := GetInstance().Raw(sql[1]).Scan(&result.Total).Error; err != nil {
			return result, err
		}
		return result, nil
	} else if types == "2" {
		sql := makeSql("approval_post_transfer", id, offset,pagination.PageSize )
		if err := GetInstance().Raw(sql[0]).Scan(&result.Result).Error; err != nil {
			return result, err
		}
		if err := GetInstance().Raw(sql[1]).Scan(&result.Total).Error; err != nil {
			return result, err
		}
		return result, nil
	} else if types == "3" {
		sql := makeSql("approval_quit", id, offset,pagination.PageSize )
		if err := GetInstance().Raw(sql[0]).Scan(&result.Result).Error; err != nil {
			return result, err
		}
		if err := GetInstance().Raw(sql[1]).Scan(&result.Total).Error; err != nil {
			return result, err
		}
		return result, nil
	} else if types == "4" {
		sql := makeSql("approval_work_overtime", id, offset,pagination.PageSize )
		if err := GetInstance().Raw(sql[0]).Scan(&result.Result).Error; err != nil {
			return result, err
		}
		if err := GetInstance().Raw(sql[1]).Scan(&result.Total).Error; err != nil {
			return result, err
		}
		return result, nil
	} else if types == "5" {
		sql := makeSql("approval_clothing", id, offset,pagination.PageSize )
		if err := GetInstance().Raw(sql[0]).Scan(&result.Result).Error; err != nil {
			return result, err
		}
		if err := GetInstance().Raw(sql[1]).Scan(&result.Total).Error; err != nil {
			return result, err
		}
		return result, nil
	} else if types == "6" {
		sql := makeSql("insure_approval", id, offset,pagination.PageSize )
		if err := GetInstance().Raw(sql[0]).Scan(&result.Result).Error; err != nil {
			return result, err
		}
		if err := GetInstance().Raw(sql[1]).Scan(&result.Total).Error; err != nil {
			return result, err
		}
		return result, nil
	} else if types == "7" {
		sql := makeSql("no_insure_approval", id, offset,pagination.PageSize )
		if err := GetInstance().Raw(sql[0]).Scan(&result.Result).Error; err != nil {
			return result, err
		}
		if err := GetInstance().Raw(sql[1]).Scan(&result.Total).Error; err != nil {
			return result, err
		}
		return result, nil
	} else if types == "8" {
		sql := makeSql("official_seal_approval", id, offset,pagination.PageSize )
		if err := GetInstance().Raw(sql[0]).Scan(&result.Result).Error; err != nil {
			return result, err
		}
		if err := GetInstance().Raw(sql[1]).Scan(&result.Total).Error; err != nil {
			return result, err
		}
		return result, nil
	} else {
		sql := makeSql("expense_declaration_approval", id, offset,pagination.PageSize )
		fmt.Println(sql)
		if err := GetInstance().Raw(sql[0]).Scan(&result.Result).Error; err != nil {
			return result, err
		}
		if err := GetInstance().Raw(sql[1]).Scan(&result.Total).Error; err != nil {
			return result, err
		}
		return result, nil
	}
}

func makeSql(table, userId string, offset, limit int) []string {
	sqlList := make([]string, 0)
	if userId != "" {
		sql1 := fmt.Sprintf("SELECT %s.*,t1.nick_name as user_name, t2.nick_name as approval_name FROM `approval_quit` left join user as t1 on %s.create_by = t1.id left join user as t2 on %s.update_by = t2.id WHERE %s.create_by = %s AND %s.`deleted_at` IS NULL ORDER BY %s.created_at desc LIMIT %d,%d", table, table, table, table,userId, table, table, offset, limit)
		sql2 := fmt.Sprintf("SELECT count(1) FROM %s left join user on %s.create_by = user.id WHERE %s.create_by = %s AND `%s`.`deleted_at` IS NULL", table, table,table,userId, table)
		sqlList = append(sqlList, sql1)
		sqlList = append(sqlList, sql2)
		return sqlList
	} else {
		sql1 := fmt.Sprintf("SELECT %s.*,t1.nick_name as user_name, t2.nick_name as approval_name FROM %s left join user as t1 on %s.create_by = t1.id  left join user as t2 on %s.update_by = t2.id WHERE %s.`deleted_at` IS NULL ORDER BY %s.created_at desc  LIMIT %d,%d", table, table, table, table, table, table,offset, limit)
		sql2 := fmt.Sprintf("SELECT count(1) FROM %s left join user on %s.create_by = user.id WHERE `%s`.`deleted_at` IS NULL", table, table,table)
		sqlList = append(sqlList, sql1)
		sqlList = append(sqlList, sql2)
		return sqlList
	}
}